<?php
defined('IN_APPS') or die('No direct access!');

/**
 * Main controller for our apps. All other controller will
 * extends this controller
 */  
class Controller {
	
	public function __construct()
	{
		global $view, $config;
		
		// set default page title
		$this->title	= $config['sitename']; 
		
		$view->assign('base_url'	, base_url());
		$view->assign('site_url'	, site_url());
		$view->assign('title'		, $this->title);
		$view->assign('sitename'	, $config['sitename']);
		$view->assign('sitedesc'	, $config['sitedesc']);
		
		// do we have message to be displayed?
		if (isset($_GET['message']) OR !empty($_GET['message'])) {
			$view->assign('message'	, $_GET['message']);
			$view->assign('type'	, $_GET['type']);
			unset($_GET['message']);
			unset($_GET['type']);
		}
		else {
			$view->assign('message', '');
		}
		
		
		// check if cookie login exists
		if (isset($_COOKIE['is_login']) OR !empty($_COOKIE['is_login'])) {
			$view->assign('is_login', '1');
			
			// set user name to show
			//$view->assign('name', $_COOKIE['firstname']);
			if (isset($_SESSION['name'])) {
				$view->assign('name', $_SESSION['name']);
			}
		}
		else {
			$view->assign('is_login', '');
			$view->assign('name', '');
		}
	}	
	
	
	public function isAdmin()
	{
		global $config;
		
		require_once MODEL_PATH . DS . 'user_model.php';
		
		// create new instance
		$userModel = new UserModel();
		
		// make sure user is loged in
		if (isset($_SESSION['user_id'])) {
			
			// check the result
			$user = $userModel->get($_SESSION['user_id']);
			
			if ($user['group_id'] == 1) {
				return true;
			}
			else {
				return false;
			}
		}
		// user not login
		else {
			return false;
		}
	}
	
}